package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.bean.UserBean;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.contract.UpdateProfileContract;
import com.thinkfit.jy.dialog.UserPopupWindow;
import com.thinkfit.jy.presenter.UpdateProfilePresenter;

/**
 * 设置用户性别弹窗界面  1男 ; 0女
 */
public class ChooseGenderFragment extends BaseFragment<UpdateProfileContract.Presenter> implements UpdateProfileContract.View {

    private int mCurrentGender;

    @NonNull
    @Override
    protected UpdateProfileContract.Presenter onCreatePresenter() {
        return new UpdateProfilePresenter(this);
    }


    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ?
//                R.layout.fragment_choose_gender_xm :
        return   R.layout.fragment_choose_gender;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.tv_ok);
        setViewsOnClickListener(R.id.llMale, R.id.llFemale);
        mCurrentGender = DataManager.Companion.getInstance().getMyInfo().getSex();
        showGender();
    }

    private void showGender() {
        if (mCurrentGender == 1) {
            setViewVisible(R.id.ivMale);
            setViewGone(R.id.ivFemale);
        } else {
            setViewVisible(R.id.ivFemale);
            setViewGone(R.id.ivMale);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llMale:
                mCurrentGender = 1;
                showGender();
                break;
            case R.id.llFemale:
                mCurrentGender = 0;
                showGender();
                break;
            case R.id.tv_ok:
                UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
                if (myInfo.getSex() == mCurrentGender) {
                    finish();
                    return;
                }
                UserPopupWindow.OnUserOlick onUserOlick = new UserPopupWindow.OnUserOlick() {
                    @Override
                    public void onClick() {
                        getPresenter().updateUser("sex", mCurrentGender);
                    }
                };
                UserPopupWindow userPopupWindow = new UserPopupWindow(getContext(), onUserOlick);
                userPopupWindow.show(getActivity());
                break;
        }
    }

    @Override
    public void updateUserSuccess(Object sex) {
        if (getView() == null) {
            return;
        }
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        myInfo.setSex((int) sex);
        DataManager.Companion.getInstance().saveMyInfo(myInfo);
        finish();
    }
}
